const loginService = require('../service/login');

// 验证用户令牌的中间件
function checkUserToken(req, res, next) {
    let token = req.headers.authorization;
    if (!token) {
        res.status(401).json({ code: 401, msg: "缺少用户令牌", data: null });
        return ;
    }
    loginService.checkUserToken(token).then(result => {
        if (result.data) {
            req.uid = result.data.uid;
            next();
        } else {
            res.status(401).json({ code: 401, msg: "令牌验证错误", data: null });
            return ;
        }
    }).catch(err => {
        res.status(401).json({ code: 401, msg: err.msg, data: null });
        return ;
    });
}

module.exports = checkUserToken;